# 多阶段构建 Dockerfile
FROM openjdk:21-jdk-slim AS builder

LABEL maintainer="OpenManus Team"
LABEL description="OpenManus Java - An open-source framework for building general AI agents"

# 安装构建依赖
RUN apt-get update && apt-get install -y \
    maven \
    && rm -rf /var/lib/apt/lists/*

# 设置工作目录
WORKDIR /app

# 复制 Maven 配置文件
COPY pom.xml .
COPY .mvn .mvn
COPY mvnw .

# 下载依赖（利用 Docker 缓存）
RUN ./mvnw dependency:go-offline -B

# 复制源代码
COPY src ./src

# 构建应用
RUN ./mvnw clean package -DskipTests -B

# 运行时镜像
FROM openjdk:21-jre-slim

LABEL maintainer="OpenManus Team"
LABEL version="1.0.0"

# 安装必要的系统包
RUN apt-get update && apt-get install -y \
    curl \
    wget \
    && rm -rf /var/lib/apt/lists/*

# 创建应用用户
RUN groupadd -r openmanus && useradd -r -g openmanus openmanus

# 设置工作目录
WORKDIR /app

# 从构建阶段复制 JAR 文件
COPY --from=builder /app/target/openmanus-java-*.jar app.jar

# 创建必要的目录
RUN mkdir -p logs workspace && \
    chown -R openmanus:openmanus /app

# 切换到应用用户
USER openmanus

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
    CMD curl -f http://localhost:8080/api/actuator/health || exit 1

# 暴露端口
EXPOSE 8080

# 设置 JVM 参数
ENV JAVA_OPTS="-XX:+UseG1GC -XX:MaxRAMPercentage=75 -XX:+UseStringDeduplication -XX:+UseCompressedOops"

# 启动应用
ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar app.jar"]
